사이트 내 전체검색
PHP
[Function] fsockopen을 이용한 웹페이지, 이미지 읽기
최고관리자
https://cmd.kr/php/754 URL이 복사되었습니다.

본문

[Function] fsockopen을 이용한 웹페이지, 이미지 읽기

외부파일이나 외부이미지를 가져올때 좋습니다. curl을 이용해도 좋지만 curl을 지원하는 계정이 얼마 없네요

fsockopen
(PHP 4, PHP 5)
[COLOR="Navy"]fsockopen ? Open Internet or Unix domain socket connection[/COLOR]

Description
resource fsockopen ( string $target [, int $port [, int &$errno [, string &$errstr [, float $timeout]]]] )

01 <?php
02 $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
03 if (!$fp) {
04    echo "$errstr ($errno)<br />\n";
05 } else {
06    $out = "GET / HTTP/1.1\r\n";
07    $out .= "Host: www.example.com\r\n";
08    $out .= "Connection: Close\r\n\r\n";
09   
10    fwrite($fp, $out);
11    while (!feof($fp)) {
12        echo fgets($fp, 128);
13    }
14    fclose($fp);
15 }
16 ?>


01 <?
02 $host = "local.paran.com"; //접속하고자하는 도메인
03 $port = "80"; //일반적인 웹서버포트
04 $fullpath = "http://local.paran.com/inc/weather/weather_include.php"; // 검색하고자 하는 페이지의 도메인 포함 전체 주소
05 if(!($fp = fsockopen($host, $port, &$errno, &$errstr, 30))) // URL에 소켓 연결
06 {
07 return array(1,"소켓에러 - 검색이 중지됨", "9");
08 exit;
09 }
10   
11 fputs($fp, "GET ".$fullpath." HTTP/1.0\r\n"."Host: $host:${port}\r\n"."User-Agent: Web 0.1\r\n"."\r\n"); // 서버에 URL 페이지 요청
12   
13 //fputs($fp, "GET $fullpath HTTP/1.0\r\n");
14 //fputs($fp, "User-Agent: Mozilla/4.0\r\n");
15 //fputs($fp, "content-type:text/html\r\n\r\n");
16   
17 while( !feof( $fp ) ) // 페이지내 모든 내용을 저장
18
19 $output .= fgets( $fp, 1024 ); 
20
21   
22 echo $output;
23 fclose( $fp ); // 소켓 해제
24 ?>


001 <?php
002   
003 function decode_header ( $str )
004 {
005    $part = preg_split ( "/\r?\n/", $str, -1, PREG_SPLIT_NO_EMPTY );
006   
007    $out = array ();
008   
009    for ( $h = 0; $h < sizeof ( $part ); $h++ )
010    {
011        if ( $h != 0 )
012        {
013            $pos = strpos ( $part[$h], ':' );
014   
015            $k = strtolower ( str_replace ( ' ', '', substr ( $part[$h], 0, $pos ) ) );
016   
017            $v = trim ( substr ( $part[$h], ( $pos + 1 ) ) );
018        }
019        else
020        {
021            $k = 'status';
022   
023            $v = explode ( ' ', $part[$h] );
024   
025            $v = $v[1];
026        }
027   
028        if ( $k == 'set-cookie' )
029        {
030                $out['cookies'][] = $v;
031        }
032        else if ( $k == 'content-type' )
033        {
034            if ( ( $cs = strpos ( $v, ';' ) ) !== false )
035            {
036                $out[$k] = substr ( $v, 0, $cs );
037            }
038            else
039            {
040                $out[$k] = $v;
041            }
042        }
043        else
044        {
045            $out[$k] = $v;
046        }
047    }
048   
049    return $out;
050 }
051   
052 function decode_body ( $info, $str, $eol = "\r\n" )
053 {
054    $tmp = $str;
055   
056    $add = strlen ( $eol );
057   
058    $str = '';
059   
060    if ( isset ( $info['transfer-encoding'] ) && $info['transfer-encoding'] == 'chunked' )
061    {
062        do
063        {
064            $tmp = ltrim ( $tmp );
065   
066            $pos = strpos ( $tmp, $eol );
067   
068            $len = hexdec ( substr ( $tmp, 0, $pos ) );
069   
070            if ( isset ( $info['content-encoding'] ) )
071            {
072                $str .= gzinflate ( substr ( $tmp, ( $pos + $add + 10 ), $len ) );
073            }
074            else
075            {
076                $str .= substr ( $tmp, ( $pos + $add ), $len );
077            }
078   
079            $tmp = substr ( $tmp, ( $len + $pos + $add ) );
080   
081            $check = trim ( $tmp );
082   
083        } while ( ! empty ( $check ) );
084    }
085    else if ( isset ( $info['content-encoding'] ) )
086    {
087        $str = gzinflate ( substr ( $tmp, 10 ) );
088    }
089   
090    return $str;
091 }
092   
093 if ( ( $io = fsockopen( "www.yahoo.com", 80, $errno, $errstr, 5 ) ) !== false )
094 {
095    $send  = "GET / HTTP/1.1\r\n";
096    $send .= "Host: www.yahoo.com\r\n";
097    $send .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";
098    $send .= "Referer: http://www.yahoo.com/\r\n";
099    $send .= "Accept: text/xml,application/xml,application/xhtml+xml,";
100    $send .= "text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,";
101    $send .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
102    $send .= "Accept-Language: en-us, en;q=0.50\r\n";
103    $send .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
104    $send .= "Connection: Close\r\n\r\n";
105   
106    fputs ( $io, $send );
107   
108    $send = '';
109   
110    do
111    {
112        $send .= fgets ( $io, 4096 );
113   
114    } while ( strpos ( $send, "\r\n\r\n" ) === false );
115   
116    $info = decode_header ( $send );
117   
118    $send = '';
119   
120    while ( ! feof ( $io ) )
121    {
122        $send .= fread ( $io, 8192 );
123    }
124   
125    fclose ( $io );
126   
127    $send = decode_body ( $info, $send );
128   
129    echo '<h3>Header Array</h3>';
130    echo '<pre>';
131    print_r ( $info );
132    echo '</pre>';
133    echo '<h3>Document Body</h3>';
134    echo $send;
135   
136 }
137   
138 ?>


또다른 방법
01 <?
03 echo $getFile['domain']."<br />";
04

댓글목록

등록된 댓글이 없습니다.

PHP
871 (5/18P)

Search

Copyright © Cmd 명령어 18.117.154.220